package se.topshelf.golftracker.core.domain;


import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.validator.constraints.Email;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.text.MessageFormat;
//testing fur alles

/**
 * User: magnusalmeby
 * Date: 2011-04-03
 */
@Entity
@Table(name = "Player", uniqueConstraints = {@UniqueConstraint(columnNames = {"golfId"})})
public class Player extends BaseEntity implements Comparable<Player>{
    @NotNull
    @Embedded
    private GolfId golfId;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @ManyToOne
    private MemberClub memberClub;
    @NotNull
    private Double handicap;
    @NotNull
    @Embedded
    private Address homeAddress;

    @Email
    private String emailAddress;


    public Player() {
    }

    public Player(GolfId golfId, String firstName, String lastName, Double handicap, Address homeAddress) {
        this.golfId = golfId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.handicap = handicap;
        this.homeAddress = homeAddress;
    }

    public GolfId getGolfId() {
        return golfId;
    }

    public String getGolfIdAsString() {
        if(golfId == null){
            return null;
        }
        return golfId.getAsString();
    }

    public void setGolfId(GolfId golfId) {
        this.golfId = golfId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public MemberClub getMemberClub() {
        return memberClub;
    }

    public void setMemberClub(MemberClub memberClub) {
        this.memberClub = memberClub;
    }

    public String getMemberClubName() {
        if (memberClub == null) {
            return null;
        }
        return memberClub.getName();
    }

    public Double getHandicap() {
        return handicap;
    }

    public void setHandicap(Double handicap) {
        this.handicap = handicap;
    }

    public Address getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(Address homeAddress) {
        this.homeAddress = homeAddress;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @Override
    public String getGuiDescription() {
        return MessageFormat.format("{0} {1} ({2})", firstName, lastName, getGolfIdAsString());
    }

    public int compareTo(Player o) {
        return new CompareToBuilder().append(lastName, o.getLastName()).
                append(firstName, o.getFirstName()).
                append(golfId, o.getGolfId()).toComparison();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Player otherPlayer = (Player) o;

       return new EqualsBuilder().append(golfId, otherPlayer.getGolfId()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(golfId).toHashCode();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Player");
        sb.append("{golfId=").append(golfId.getAsString());
        sb.append(", firstName='").append(firstName).append('\'');
        sb.append(", lastName='").append(lastName).append('\'');
        sb.append(", memberClub=").append(memberClub);
        sb.append(", handicap=").append(handicap);
        sb.append('}');
        return sb.toString();
    }
}
